package hot100;

import java.util.*;

public class groupAnagrams2 {
    public List<List<String>> groupAnagrams(String[] strs) {
        // Hash表，key：词，value：字母异位词
        Map<String, List<String>> hash = new HashMap<>();


        for (int i = 0; i < strs.length; i++) {
            // 将字符串排序，比较key，是否添加List
            char[] s = strs[i].toCharArray();
            Arrays.sort(s);
            String tem = new String(s);
            //比较Key
            List<String> n = hash.getOrDefault(tem, new ArrayList<>());
            //更新value
            n.add(strs[i]);
            hash.put(tem, n);
        }
        return new ArrayList<List<String>>(hash.values());
    }
}
